package com.example.lock.乐观锁.cas;

import java.util.concurrent.atomic.AtomicInteger;

public class TestCAS {

    //使用AtomicInteger定义a，默认值0
    static AtomicInteger a = new AtomicInteger();

    public static void main(String[] args) {
        TestCAS testCAS = new TestCAS();
        Thread[] threads = new Thread[5];
        //定义五个线程，每个线程加10
        for(int i = 0;i<5;i++){
            threads[i] = new Thread(()->{
                try {
                    for(int j = 0;j<10;j++){
                        System.out.println(a.incrementAndGet());
                        Thread.sleep(500);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
            threads[i].start();
        }
    }
}
